<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_form" :model="loginFormData" :rules="rules" ref="loginForm">
          <h1>HELLO</h1>
          <h2>欢迎来到大林甄选学习平台</h2>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="loginFormData.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginFormData.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              class="login_btn"
              :loading="loading"
              type="primary"
              size="default"
              @click="login"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { reactive, toRefs, ref, onMounted, useTemplateRef } from 'vue'
import { User, Lock, Loading } from '@element-plus/icons-vue'
import type { FormInstance } from 'element-plus'
import { ElNotification, ElForm } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/modules/user'
//获取el-form组件
//let loginForms = ref()
let loginForm = useTemplateRef<FormInstance>('loginForm')
//收集表单数据
let loginFormData = reactive({ username: 'admin', password: '111111' })
//按钮加载效果
let loading = ref(false)
//用户仓库
let userStore = useUserStore()

let router = useRouter()
let route = useRoute()

const login = async () => {
  loading.value = true
  await loginForm.value?.validate()
  try {
    ElNotification({
      title: 'Hi',
      message: '登录成功',
      type: 'success',
    })
    loading.value = false

    await userStore.userLogin(loginFormData)
    //跳转(如果路径中有redirect参数，则跳转到redirect参数，否则跳转到首页)
    const redirect: any = route.query.redirect
    router.push({ path: redirect || '/' })
  } catch (error) {
    ElNotification.error((error as Error).message)
    loading.value = false
  }
}

//自定义校验规则
const validatorUserName = (rule: any, value: any, callback: any) => {
  if (!value) {
    callback(new Error('请输入账号'))
  } else if (value.length < 5) {
    callback(new Error('账号长度不能少于5位'))
  } else {
    callback()
  }
}

const validatorPassWord = (rule: any, value: any, callback: any) => {
  if (!value) {
    callback(new Error('请输入密码'))
  } else if (value.length < 6) {
    callback(new Error('密码长度不能少于6位'))
  } else {
    callback()
  }
}

//定义表单检验规则配置对象
const rules = {
  username: [{ trigger: 'blur', validator: validatorUserName }],
  password: [{ trigger: 'blur', validator: validatorPassWord }],
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  h2 {
    margin: 20px 0;
  }

  .login_form {
    width: 70%;
    height: 40vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    position: relative;
    top: 30vh;
    padding: 40px;
    color: white;
  }

  .login_btn {
    width: 100%;
  }
}
</style>
